const grpc = require("@grpc/grpc-js");
const message_proto = require("./proto");
const const_module = require("./const");
const { v4: uuidv4 } = require("uuid");
const email_module = require("./email");
const redis_module = require("./redis");

/**
 * GetVarifyCode grpc响应获取验证码的服务
 * @param {*} call 为grpc请求
 * @param {*} callback 为grpc回调
 * @returns
 */
async function GetVarifyCode(call, callback) {
  console.log("email is ", call.request.email);
  try {
    let query_res = await redis_module.GetRedis(
      const_module.code_prefix + call.request.email
    );
    console.log("query_res is ", query_res);
    if (query_res == null) {
    }
    let uniqueId = query_res;
    if (query_res == null) {
      uniqueId = uuidv4();
      if (uniqueId.length > 4) {
        // uniqueId = uniqueId.substring(0, 4);
      }
      let bres = await redis_module.SetRedisExpire(
        const_module.code_prefix + call.request.email,
        uniqueId,
        600
      );
      if (!bres) {
        callback(null, {
          email: call.request.email,
          error: const_module.Errors.RedisErr,
        });
        return;
      }
    }

    console.log("uniqueId is ", uniqueId);
    let text_str = "您的验证码为" + uniqueId + "请600s内完成注册";
    //发送邮件
    let mailOptions = {
      from: "18175549596@163.com",
      to: call.request.email,
      subject: "验证码",
      text: text_str,
    };

    let send_res = await email_module.SendMail(mailOptions);
    console.log("send res is ", send_res);

    callback(null, {
      email: call.request.email,
      error: const_module.Errors.Success,
    });
  } catch (error) {
    console.log("catch error is ", error);

    callback(null, {
      email: call.request.email,
      error: const_module.Errors.Exception,
    });
  }
}
// async function GetVarifyCode(call, callback) {
//   console.log("email is ", call.request.email);
//   try {
//     let query_res = await redis_module.GetRedis(
//       const_module.code_prefix + call.request.email
//     );
//     console.log("query_res is ", query_res);

//     // 不存在，就设置加入到redis中，并设置过期时间
//     // 如果600s之内多次请求，因为验证码被缓存在redis中，所以会被复用返回给客户端
//     let uniqueId = query_res;
//     if (query_res == null) {
//       uniqueId = uuidv4();
//       // if (uniqueId.length > 4) uniqueId = uniqueId.substring(0, 4);
//       let bres = await redis_module.SetRedisExpire(
//         const_module.code_prefix + call.request.email,
//         uniqueId,
//         600
//       );
//       if (bres == false) {
//         callback(null, {
//           email: call.request.email,
//           error: const_module.Errors.RedisErr,
//         });
//         return;
//       }
//     }

//     console.log("uniqueId is ", uniqueId);
//     let text_str = "您的验证码为" + uniqueId + "请600秒内完成注册";
//     //发送邮件
//     let mailOptions = {
//       from: "18175549596@163.com",
//       to: call.request.email,
//       subject: "验证码",
//       text: text_str,
//     };

//     let send_res = await email_module.SendMail(mailOptions);
//     console.log("send res is ", send_res);

//     callback(null, {
//       email: call.request.email,
//       error: const_module.Errors.Success,
//     });
//   } catch (error) {
//     console.log("catch error is ", error);

//     callback(null, {
//       email: call.request.email,
//       error: const_module.Errors.Exception,
//     });
//   }
// }

function main() {
  var server = new grpc.Server();
  server.addService(message_proto.VarifyService.service, {
    GetVarifyCode: GetVarifyCode,
  });
  server.bindAsync(
    "0.0.0.0:50051",
    grpc.ServerCredentials.createInsecure(),
    () => {
      server.start();
      console.log("grpc server started");
    }
  );
}

main();
